//设置界面状态

import ampub from 'ampub';
import { showOrHideIsdepfornewcard } from ".";
import constants from '../../constants';
import { simpleTraceabilityDominate, stepLease } from './usufructAssetHandler';


const {
	FIELDS,
	UISTATE,
    BILL_SOURCE_TYPES,
	CARD_BROWER_BTNS: browseBtns,
	CARD_EDIT_BTNS: editBtns,
	CARD_BLANK_BTNS: blankBtns,
	BTN_AREA,
	CARD_BTNS: {
		HEAD: { EDIT: TOPEDIT, QueryAboutCard, QueryAboutBusiness, LEASELIABILITIES },
		TABLE_BODY,
		TABLE_HEAD,
		TABLE_HEAD: { INTEREST: { DAILY_INTEREST: daily_interest_btn, MONTHLY_INTEREST: monthly_interest_btn } }
	},
	AREA: {
		CARD: {
            USEDEPT_TAB: usedept_tab,
	    LRZX_TAB: lrzx_tab,
			SUBEQUIP_TAB: subequip_tab,
			BASIC_INFO_AREA: basicInfoForm,
			FINANCE_AREA: financeForm,
			USUFRUCTASSET_AREA: usufructassetForm,
			RENTPAYPLAN_TAB,
			DEPACCRUAL_TAB_MONTH,
			STEPRENTSTANDARD_TAB: steprentstandard_tab,
			SUBEQUIP_TAB,
			USEDEPT_TAB,
			LRZX_TAB,
			LEASE_CHANGE_TAB
		}
	}
} = constants;
const { utils: { cardUtils: { setHeadAreaData, setBtnsVisibleBySagaStatus } } } = ampub;

//切换按钮状态
export default function setStatus(props, status) {
	const { button, form, cardTable } = props;
	const { ADD, EDIT, BROWSE, BLANK } = UISTATE;
	status && props.setUrlParam({ status });
	status = status || props.getUrlParam('status');

	//按钮的显示状态
	if (status === BLANK) {
		this.resetOnAdd();

		button.setButtonVisible(browseBtns, false);
		button.setButtonVisible(editBtns, false);
		button.setButtonVisible(blankBtns, true);
	} else if (status === EDIT || status === ADD) {
		//编辑态
		button.setButtonVisible(browseBtns, false);
		button.setButtonVisible(editBtns, true);
		//props.cardPagination.setCardPaginationVisible('cardPaginationBtn', false);

		//多使用部门页签按钮及编辑性控制
		let usedep_flag = props.form.getFormItemsValue(basicInfoForm, 'usedep_flag').value;
		//表头使用部门字段控制
		form.setFormItemsDisabled(basicInfoForm, { pk_usedept: usedep_flag });
		// 表头使用部门默认是否可空
		form.setFormItemsRequired(basicInfoForm, { pk_usedept: !usedep_flag });
		// 多使用部门页签上的按钮及编辑性
		let btns = Object.values(TABLE_HEAD.USEDEPT_TAB);
		button.setButtonVisible(btns, usedep_flag);
        button.setButtonVisible(Object.values(TABLE_HEAD.SUBEQUIP_TAB), true);
        
		if (status === EDIT) {
			form.setFormItemsDisabled(basicInfoForm, { pk_org: true });
		} else {
			form.setFormItemsDisabled(basicInfoForm, { pk_org: false });
		}
	} else {
		//浏览态
		button.setButtonVisible(browseBtns, true);
		button.setButtonVisible(editBtns, false);

        const asset_state = form.getFormItemsValue(basicInfoForm, 'asset_state').value;

		//联查 设备卡片按钮 
		const pk_equip = form.getFormItemsValue(basicInfoForm, 'pk_equip').value;
        const equip_flag = !!pk_equip && 'exist' == asset_state
        /* 减少态或未关联设备卡片禁用 联查设备卡片按钮 */
        button.setButtonDisabled(QueryAboutCard, !equip_flag);
        
        //获取卡片来源交易类型 有默认值handin
        let transi_type_source = form.getFormItemsValue(basicInfoForm, 'transi_type_source').value;
        if(transi_type_source === BILL_SOURCE_TYPES.handin_src){
            transi_type_source = null;
        }
        // 获取卡片来源单据类型
        const bill_source = form.getFormItemsValue(basicInfoForm, 'bill_source').value;

        let type_src = transi_type_source || bill_source;
        //获取主键
	    let pk_src = form.getFormItemsValue(basicInfoForm, 'pk_bill_src').value;
        if(type_src && pk_src && !type_src.startsWith(BILL_SOURCE_TYPES.equip_src)){
            button.setButtonDisabled(QueryAboutBusiness, false); //  联查 单据追溯
        }else{
            button.setButtonDisabled(QueryAboutBusiness, true); //  联查 单据追溯
        }
	}

	/* 使用权按钮状态 */
	setUsufructassetBtns.call(this, status);

	const formStatus = status == undefined || status == BLANK ? BROWSE : status;
	form.setFormStatus(basicInfoForm, formStatus);
	form.setFormStatus(financeForm, formStatus);

	const tableStatus = [ ADD, EDIT ].includes(status) ? EDIT : BROWSE;
	cardTable.setStatus(RENTPAYPLAN_TAB, tableStatus);
	cardTable.setStatus(DEPACCRUAL_TAB_MONTH, tableStatus);
	cardTable.setStatus(steprentstandard_tab, tableStatus);
	cardTable.setStatus(SUBEQUIP_TAB, tableStatus);
	cardTable.setStatus(USEDEPT_TAB, tableStatus);
	cardTable.setStatus(LRZX_TAB, tableStatus);

	//设置标题区域状态
	setHeadAreaData.call(this, props, { status });

	// 根据 saga 状态控制按钮状态
	setBtnsVisibleBySagaStatus.call(this, props, {
		formAreaCode: basicInfoForm,
		headBtnAreaCode: BTN_AREA.CARD_HEAD,
		billpkname: FIELDS.HEAD_PK
	});

	//控制表体页签肩部按钮
	setBodyBtnsEnable(props, SUBEQUIP_TAB);
	setBodyBtnsEnable(props, USEDEPT_TAB);
	setBodyBtnsEnable(props, LRZX_TAB);
	setBodyBtnsEnable(props, steprentstandard_tab);
	setBodyBtnsEnable(props, LEASE_CHANGE_TAB);

	const metaData = props.meta.getMeta();

	// 资产租入合同和变动单特殊处理
	if (status === EDIT) {
		// 资产租入合同，禁止编辑阶梯租金
		if (this.bill_source_4A3A) {
			button.setButtonVisible(Object.values(TABLE_HEAD.LEASE_CHANGE_TAB), false);
			button.setButtonVisible(Object.values(TABLE_BODY.LEASE_CHANGE_TAB), false);
			button.setButtonVisible(Object.values(TABLE_HEAD.STEPRENTSTANDARD_TAB), false);
			button.setButtonVisible(Object.values(TABLE_BODY.STEPRENTSTANDARD_TAB), false);
			cardTable.setStatus(steprentstandard_tab, BROWSE);
			cardTable.setStatus(RENTPAYPLAN_TAB, BROWSE);
        }
        
        //判断使用部门、附属设备页签是否可以编辑
		let isAllowed = this.isAllowChangeHeadWhenEdit();

		//当前是单使用部门 或者 存在后续业务操作，不允许编辑使用部门页签
		const usedep_flag = form.getFormItemsValue(basicInfoForm, 'usedep_flag').value;
		if (!usedep_flag || !isAllowed) {
			// 设置表体页签不能编辑
			// 多使用部门页签上的按钮不可以使用
			let btns = Object.values(TABLE_HEAD.USEDEPT_TAB);
			button.setButtonVisible(btns, false);
			cardTable.setStatus(usedept_tab, UISTATE.BROWSE);
		}

		// 有后续操作，仅允许编辑阶梯租金的备注
		if (!isAllowed) {
			cardTable.setStatus(steprentstandard_tab, BROWSE);
			cardTable.setColEditableByKey(steprentstandard_tab, 'remarks', false);
		}

		// [ 'remarks', 'def1', 'def2', 'def3', 'def4', 'def5' ]
		// 计提利息明细，按月计息方式下，控制计提利息明细的备注和自定义字段
		const { [DEPACCRUAL_TAB_MONTH]: { items: depaccrual_tab_month_items } } = metaData;
		const depaccrual_tab_month_items_attrcode = depaccrual_tab_month_items
			.filter((item) => item.visible)
			.map((item) => item.attrcode);
		cardTable.setColEditableByKey(DEPACCRUAL_TAB_MONTH, depaccrual_tab_month_items_attrcode, true);
		cardTable.setColEditableByKey(DEPACCRUAL_TAB_MONTH, TABLE_BODY.DEPACCRUAL_TAB_REMARKS_DEF.REMARKS_DEF, false);

		// 资产租入合同，禁止编辑计提利息明细的备注
		if (this.bill_source_4A3A) {
			cardTable.setColEditableByKey(DEPACCRUAL_TAB_MONTH, 'remarks', true);
		}
	}

    /* 建卡当期计提控制 */
    showOrHideIsdepfornewcard.call(this);
    
}
/**
 * 设置表体肩部按钮是否可用
 * @param {*} props
 * @param {*} moduleId
 */
export function setBodyBtnsEnable(props, moduleId) {
	const { form, button, cardTable } = props;
	const { ADD, EDIT } = UISTATE;
	let pk_org = form.getFormItemsValue(basicInfoForm, 'pk_org');
	let btns = {
		addLine: 'AddLine',
		delLine: 'DelLine',
		batchAlter: 'BatchAlter'
	};
	if (moduleId === USEDEPT_TAB) {
		//多使用部门
		btns.addLine = TABLE_HEAD.USEDEPT_TAB.ADD_LINE;
		btns.delLine = TABLE_HEAD.USEDEPT_TAB.DELETE_LINE;
		btns.batchAlter = TABLE_HEAD.USEDEPT_TAB.BATCH_ALTER;
	} else if (moduleId === LRZX_TAB) {
		//利润中心部门
		btns.addLine = TABLE_HEAD.LRZX_TAB.ADD_LINE;
		btns.delLine = TABLE_HEAD.LRZX_TAB.DELETE_LINE;
		if(EDIT == props.getUrlParam('status') || ADD == props.getUrlParam('status')){
		//就是要强制可用
		button.setButtonDisabled([ btns.addLine, btns.batchAlter, btns.interest ], false);
		let checkedRows = [];
		let num = cardTable.getNumberOfRows(moduleId, false);
		if (num > 0) {
			checkedRows = cardTable.getCheckedRows(moduleId);
		}
		button.setButtonDisabled([ btns.delLine ], !(checkedRows && checkedRows.length > 0));
		return;
		}
		

	}else if (moduleId === SUBEQUIP_TAB) {
		//附属设备
		btns.addLine = TABLE_HEAD.SUBEQUIP_TAB.ADD_LINE;
		btns.delLine = TABLE_HEAD.SUBEQUIP_TAB.DELETE_LINE;
		btns.batchAlter = TABLE_HEAD.SUBEQUIP_TAB.BATCH_ALTER;
	} else if (moduleId === steprentstandard_tab) {
		//阶梯租金标准
		btns.addLine = TABLE_HEAD.STEPRENTSTANDARD_TAB.ADD_LINE;
		btns.delLine = TABLE_HEAD.STEPRENTSTANDARD_TAB.DELETE_LINE;
		btns.batchAlter = TABLE_HEAD.STEPRENTSTANDARD_TAB.BATCH_ALTER;
	} else if (moduleId === LEASE_CHANGE_TAB) {
		//租赁变更
		btns.addLine = TABLE_HEAD.LEASE_CHANGE_TAB.ADD_LINE;
		btns.delLine = TABLE_HEAD.LEASE_CHANGE_TAB.DELETE_LINE;
	}

	if (!pk_org || !pk_org.value) {
		button.setButtonDisabled([ btns.addLine, btns.delLine, btns.batchAlter, btns.interest ], true);
	} else {
		button.setButtonDisabled([ btns.addLine, btns.batchAlter, btns.interest ], false);
		let checkedRows = [];
		let num = cardTable.getNumberOfRows(moduleId, false);
		if (num > 0) {
			checkedRows = cardTable.getCheckedRows(moduleId);
		}
		button.setButtonDisabled([ btns.delLine ], !(checkedRows && checkedRows.length > 0));
	}
}

//使用权资产相关页签按钮及编辑性控制
export function setUsufructassetBtns(status) {
	const { props: { form, button }, state: { usufructasset_flag } } = this;
	const { ADD, EDIT, BROWSE, BLANK } = UISTATE;

	//计算租赁负债按钮
	button.setButtonVisible(LEASELIABILITIES, false);
	if ([ ADD, EDIT ].includes(status) && usufructasset_flag) {
		button.setButtonVisible(LEASELIABILITIES, true);
	}

	//阶梯租金标准
	button.setButtonVisible(Object.values(TABLE_HEAD.STEPRENTSTANDARD_TAB), false);
	// 在没有后续操作，启用阶梯租金，编辑态时允许编辑
	if ([ ADD, EDIT ].includes(status) && usufructasset_flag && this.isAllowChangeHeadWhenEdit()) {
		let step_rent = form.getFormItemsValue(basicInfoForm, 'step_rent').value;
		if (step_rent) {
			button.setButtonVisible(Object.values(TABLE_HEAD.STEPRENTSTANDARD_TAB), true);
		}
	}
	//租赁变更按钮
	if ([ BROWSE ].includes(status)) {
		button.setButtonVisible(TABLE_HEAD.LEASE_CHANGE, usufructasset_flag);
		button.setButtonVisible(Object.values(TABLE_HEAD.LEASE_CHANGE_TAB), usufructasset_flag);
	} else {
		button.setButtonVisible(TABLE_HEAD.LEASE_CHANGE, false);
		button.setButtonVisible(Object.values(TABLE_HEAD.LEASE_CHANGE_TAB), false);
	}

    /* 简单追溯控制 */
    if ([ EDIT ].includes(status) && usufructasset_flag) {
        simpleTraceabilityDominate.call(this);
    }

     /* 按阶梯租金计租 */
     if ([ EDIT ].includes(status) && usufructasset_flag) {
        stepLease.call(this);
    }

}
